package com.whisper.application.service.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.whisper.application.service.domain.vo.VrActVo;
import com.whisper.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whisper.application.service.mapper.VrActMapper;
import com.whisper.application.service.domain.VrAct;
import com.whisper.application.service.service.IVrActService;

import static com.whisper.common.security.util.SecurityUtils.getUsername;

/**
 * 志愿活动Service业务层处理
 * 
 * @author whisper
 * @date 2024-01-07
 */
@Service
public class VrActServiceImpl implements IVrActService 
{
    @Autowired
    private VrActMapper vrActMapper;

    /**
     * 查询志愿活动
     * 
     * @param id 志愿活动主键
     * @return 志愿活动
     */
    @Override
    public VrAct selectVrActById(Long id)
    {
        return vrActMapper.selectVrActById(id);
    }

    /**
     * 查询志愿活动列表
     * 
     * @param vrAct 志愿活动
     * @return 志愿活动
     */
    @Override
    public List<VrAct> selectVrActList(VrAct vrAct)
    {
        return vrActMapper.selectVrActList(vrAct);
    }

    @Override
    public List<VrActVo> selectVrActVoList(VrAct vrAct)
    {
        return vrActMapper.selectVrActVoList(vrAct);
    }

    /**
     * 新增志愿活动
     * 
     * @param vrAct 志愿活动
     * @return 结果
     */
    @Override
    public int insertVrAct(VrAct vrAct)
    {
        vrAct.setCreateTime(DateUtils.getNowDate());
        vrAct.setCreateBy(getUsername());
        return vrActMapper.insertVrAct(vrAct);
    }

    /**
     * 修改志愿活动
     * 
     * @param vrAct 志愿活动
     * @return 结果
     */
    @Override
    public int updateVrAct(VrAct vrAct)
    {
        vrAct.setUpdateTime(DateUtils.getNowDate());
        vrAct.setUpdateBy(getUsername());
        return vrActMapper.updateVrAct(vrAct);
    }

    /**
     * 批量删除志愿活动
     * 
     * @param ids 需要删除的志愿活动主键
     * @return 结果
     */
    @Override
    public int deleteVrActByIds(Long[] ids)
    {
        return vrActMapper.deleteVrActByIds(ids);
    }

    /**
     * 删除志愿活动信息
     * 
     * @param id 志愿活动主键
     * @return 结果
     */
    @Override
    public int deleteVrActById(Long id)
    {
        return vrActMapper.deleteVrActById(id);
    }

    @Override
    public void checkAndUpdateStatu(List<VrAct> vrActs) {
        List<VrAct> collect = vrActs.stream().filter(vrAct -> vrAct.getEndTime().before(new Date())).collect(Collectors.toList());
        collect = collect.stream().peek(vrAct -> {
            vrAct.setStatus("2");
            updateVrAct(vrAct);
        }).collect(Collectors.toList());
    }

    @Override
    public int selectActCount() {
        return vrActMapper.selectActCount();
    }

    @Override
    public List<VrActVo> selectVrActVoListLimit(VrAct vrAct, int i) {
        return vrActMapper.selectVrActVoListLimit(vrAct, i);
    }

    @Override
    public List<VrAct> selectVrActBatchIds(List<Long> list) {
        return vrActMapper.selectVrActBatchIds(list.stream().toArray(Long[]::new));
    }
}
